package com.nuanshui.heatedloan.configBehavior;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.behavior.BehaviorAttribute;
import com.nuanshui.heatedloan.baseservice.facade.ClientVersionService;
import com.nuanshui.heatedloan.baseservice.model.ClientVersion;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.service.services.CommonTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * Created by zxh on 2016/11/19.
 */
@Repository
@BehaviorAttribute
public class ClientVersionConfigBehavior extends ConfigBehavior {


	@Reference(url = GlobalParam.BASESERVICE_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.BASESERVICE_DUBBO_SERVER_VERSION)
	private ClientVersionService clientVersionService;
    
    @Autowired
    private CommonTool commonTool;
    
    @Override
	public void doInvoke() {
    	
    	//保存版本信息
		Map<String, Object> map = getMap();
		
		//获取客户端版本类型
		String type = commonTool.getClient().toString();
		
		//获取新版本信息
		ClientVersion cv = clientVersionService.getNewClientVersion(type);
		
		//f为true表示有新版本，可以升级
		boolean f = false;
		if (cv != null) {
			if (cv.getVersionNum() != null) {
				f = commonTool.compareVersion(cv.getVersionNum());
			}
		}
		
		if (f) {
			map.put("updateVersion", cv.getVersionNum());
			map.put("versionDes", cv.getDes());
			map.put("msg", "有新版本是否升级");
		}
	}
    
}
